import {String}from '../../../../utils/util.js'

var apiHost = wx.getStorageSync('apiHost');

var addressId = '';

var provinces = [];//省

var citys = [];//城市

var countys = [];//区县

var index = [0, 0, 0];

var cellId;

var t = 0;
var show = false;
var moveY = 200;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: '',

    locationInput:'',
    addressId:'',//地址id
    name:'',
    addressInfo:'',
    phone:'',
    city:'',
    zipCode:'',

    cityId:'894833960494825472',
    show: show,
    provinces: provinces,
    citys: citys,
    countys: countys,
    value: [0, 0, 0],

    province:'',
    city:'',
    county:'',
  },
  //滑动事件
  bindChange: function (e) {
    var val = e.detail.value
    var data = this.data
    var that = this
    console.log('huadong',e)
    // // console.log(e)
    //判断滑动的是第几个column
    //若省份column做了滑动则定位到地级市和区县第一位
    if (index[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
      getCityArr(that.data.provinces[val[0]].id, val[0], this);//获取地级市数据
      getCountyInfo(that.data.citys[0].cityId, val[0],val[1],val[2], this);//获取区县数据
    } 
    else {    //若省份column未做滑动，地级市做了滑动则定位区县第一位
      if (index[1] != val[1]) {
        val[2] = 0;
        console.log('lalala', val[1])
        getCountyInfo(that.data.citys[val[1]].cityId, val[0], val[1],val[2], this);//获取区县数据
      }else{
        getCountyInfo(that.data.citys[val[1]].cityId, val[0], val[1], val[2], this);//获取区县数据
      }
    }
    index = val;
    console.log(index + " => " + val);

    console.log('zuihou', data.countys[val[2]].partId)
    //更新数据
    that.setData({
      value: [val[0], val[1], val[2]],
      cityId: data.countys[val[2]].partId,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    cellId = options.cellId;
    var date = new Date()
    console.log(date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日");

    var userId = wx.getStorageSync('userId');
    console.log('添加地址的userId', userId);
    var that = this;

    that.setData({
      userId: userId
    })
    //把上个界面带过来的地址ID赋值
    if (options.addressId){
      that.setData({
        addressId: options.addressId,
      });
    }
    //获取地址详情
    that.getAddressData(options.addressId);
      //获取省份数据
      getProvinceData(that);


  },
  /**
   * form表单
   */
  formSubmit:function(e){
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var that = this;
    that.setData({
      name:e.detail.value.userName,
      addressInfo: e.detail.value.userAddressInfo,
      phone:e.detail.value.userPhone,
      zipCode:e.detail.value.userZipCode,
      // city:e.detail.value.city,
    });
    if (String.isBlank(that.data.name) || String.isBlank(that.data.phone) || String.isBlank(that.data.addressInfo) || String.isBlank(that.data.city) ) {
      wx.showToast({
        title: '请输入完整信息',
        icon : 'none',
      })
    } else {
      wx.showLoading({
        title: '请稍等',
      })
      console.log('提交参数',that.data)
      wx.request({
        url: apiHost + 'hpp/user/address/addOrEdit',
        data: {
          userId: that.data.userId,
          addressId: that.data.addressId,
          regionId: that.data.cityId,
          reciveName: that.data.name,
          recivePhone: that.data.phone,
          zipCode: that.data.zipCode,
          address: that.data.addressInfo,
        },
        success: function (res) {
          console.log('添加返回',res)
          if (res.data.code == '10000') {
            wx.showToast({
              title: '添加成功',
            })
            wx.navigateBack({

            })
          } else {
            wx.showToast({
              title: ('cuowu',res.data.msg),
              icon: 'none',
            })
          }
        }
      })
    }
  },
  /**
   * 获取地址详情
   */
  getAddressData:function(id){
    var that = this;
    wx.request({
      url: apiHost + 'hpp/user/address/detail',
      data:{
        addressId : id,
      },
      success:function(res){
        console.log('地址详情', res.data),
        that.setData({
          name: res.data.data.reciveName,
          addressInfo: res.data.data.address,
          phone: res.data.data.recivePhone,
          zipCode: res.data.data.zipCode,
        })
      }
    })
  },
  /**
   * 删除地址
   */
  deletAddress:function(){

    var that = this;
    wx.showLoading({
      title: '请稍等',
    }),
      
    wx.request({
      url: apiHost + '/hpp/user/address/del',
      data: {
        userId: that.data.userId,
        addressId : that.data.addressId,
      },
      success : function(res){
        console.log('删除', res)
        wx.hideLoading();
        wx.showToast({
          title: res.data.msg,
          icon : 'none',
        })
        if(res.data.code == '10000'){
          console.log('删除', res)
          wx.navigateBack({
            
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.animation = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 0,
      timingFunction: "ease",
      delay: 0
    }
    )
    this.animation.translateY(200 + 'vh').step();
    this.setData({
      animation: this.animation.export(),
      show: show
    })
  },
  //移动按钮点击事件
  translate: function (e) {
    if (t == 0) {
      moveY = 0;
      show = false;
      t = 1;
    } else {
      moveY = 200;
      show = true;
      t = 0;
    }
    // this.animation.translate(arr[0], arr[1]).step();
    animationEvents(this, moveY, show);

  },
  //隐藏弹窗浮层
  hiddenFloatView(e) {
    console.log(e);
    moveY = 200;
    show = true;
    t = 0;
    animationEvents(this, moveY, show);

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})

//动画事件
function animationEvents(that, moveY, show) {
  console.log("moveY:" + moveY + "\nshow:" + show);
  that.animation = wx.createAnimation({
    transformOrigin: "50% 50%",
    duration: 400,
    timingFunction: "ease",
    delay: 0
  }
  )
  that.animation.translateY(moveY + 'vh').step()

  that.setData({
    animation: that.animation.export(),
    show: show
  })

}

// ---------------- 分割线 ---------------- 

//获取省份数据
function getProvinceData(that) {

  wx.request({
    url: apiHost + '/hpp/region/province',
    success: function (res) {  
      console.log('sheng',res.data.data);
      var pId = res.data.data[0].id;
      that.setData({
        provinces: res.data.data,
      })
      getCityArr(pId, 0,that);
    }
  })
  //初始化调一次
  that.setData({
    province: "北京",
    city: "北京",
    county: "东城区",
    cityId:"894833960494825472"
  })
}

// 获取地级市数据
function getCityArr(pId, count,that) {
var cityId = '';
  wx.request({
    url: apiHost + '/hpp/region/city',
    data: {provinceId :pId },
    success: function (res) {
      cityId = res.data.data[0].cityId;
      that.setData({
        citys: res.data.data,
        // value : [count,0,0],
      })
      getCountyInfo(cityId, count,0,0,that);
    }
  })
  if (citys.length == 0) {
    citys[0] = { name: '' };
  }
  
}

// 获取区县数据
function getCountyInfo(cityId,column0,column1,column2, that) {
  var appData = that.data;
  wx.request({
    url: apiHost + '/hpp/region/part',
    data: { cityId: cityId },
    success: function (res) {

      console.log('gundong', appData.citys[column1].cityName)
      that.setData({
        province:'',
        city:'',
        county:'',
        countys:  res.data.data,
        cityId: res.data.data[column2].partId,
        province: appData.provinces[column0].regionName,
        city: appData.citys[column1].cityName,
        county: res.data.data[column2].partName,
      })
    }
  })
  
}